package com.inepex.inechart.client.cartesian;

import com.inepex.inechart.client.displayengine.CartesianRectangleTile;
import com.inepex.inechart.client.displayengine.RectangleTile;
import com.inepex.inechart.client.displayengine.TileOwner;
import com.inepex.inechart.client.event.DataEntrySelectionEvent;
import com.inepex.inechart.client.event.DataEntrySelectionHandler;
import com.inepex.inechart.client.event.DataSetChangeEvent;
import com.inepex.inechart.client.event.DataSetChangeHandler;
import com.inepex.inechart.client.event.ViewportChangeEvent;
import com.inepex.inechart.client.event.ViewportChangeHandler;

class CartesianModuleInnerEventHandler implements ViewportChangeHandler, DataSetChangeHandler, DataEntrySelectionHandler, TileOwner{

	/**
	 * 
	 */
	private final CartesianChartModuleGWT CartesianChartModuleGWT;

	/**
	 * @param CartesianChartModuleGWT
	 */
	CartesianModuleInnerEventHandler(CartesianChartModuleGWT CartesianChartModuleGWT) {
		this.CartesianChartModuleGWT = CartesianChartModuleGWT;
	}

	public boolean isAddressed(ViewportChangeEvent event){
		if(event.getAddressedModules() == null || event.getAddressedModules().contains(this.CartesianChartModuleGWT)){
			return true;
		}
		else{
			return false;
		}
	}

	@Override
	public void onMove(ViewportChangeEvent event, double dx, double dy) {
		if(!this.CartesianChartModuleGWT.canHandleEvents)
			return;
		if(!isAddressed(event))
			return;
		if(this.CartesianChartModuleGWT.getXAxis().getModulToAlign() == this.CartesianChartModuleGWT){
			this.CartesianChartModuleGWT.getXAxis().setMin(this.CartesianChartModuleGWT.getXAxis().getMin() + dx);
			this.CartesianChartModuleGWT.getXAxis().setMax(this.CartesianChartModuleGWT.getXAxis().getMax() + dx);
		}
		if(this.CartesianChartModuleGWT.getYAxis().getModulToAlign() == this.CartesianChartModuleGWT){
			this.CartesianChartModuleGWT.getYAxis().setMin(this.CartesianChartModuleGWT.getYAxis().getMin() + dy);
			this.CartesianChartModuleGWT.getYAxis().setMax(this.CartesianChartModuleGWT.getYAxis().getMax() + dy);
		}
		this.CartesianChartModuleGWT.onMove(event, dx, dy);
	}

	@Override
	public void onSet(ViewportChangeEvent event, double xMin, double yMin,
			double xMax, double yMax) {
		if(!this.CartesianChartModuleGWT.canHandleEvents)
			return;
		if(!isAddressed(event))
			return;
		if(this.CartesianChartModuleGWT.getXAxis().getModulToAlign() == this.CartesianChartModuleGWT){
			this.CartesianChartModuleGWT.getXAxis().setMin(xMin);
			this.CartesianChartModuleGWT.getXAxis().setMax(xMax);
		}
		if(this.CartesianChartModuleGWT.getYAxis().getModulToAlign() == this.CartesianChartModuleGWT){
			this.CartesianChartModuleGWT.getYAxis().setMin(yMin);
			this.CartesianChartModuleGWT.getYAxis().setMax(yMax);
		}
		this.CartesianChartModuleGWT.onSet(event, xMin, yMin, xMax, yMax);
	}

	@Override
	public void onMoveAlongX(ViewportChangeEvent event, double dx) {
		if(!this.CartesianChartModuleGWT.canHandleEvents)
			return;
		if(!isAddressed(event))
			return;
		if(this.CartesianChartModuleGWT.getXAxis().getModulToAlign() == this.CartesianChartModuleGWT){
			this.CartesianChartModuleGWT.getXAxis().setMin(this.CartesianChartModuleGWT.getXAxis().getMin() + dx);
			this.CartesianChartModuleGWT.getXAxis().setMax(this.CartesianChartModuleGWT.getXAxis().getMax() + dx);

		}
		this.CartesianChartModuleGWT.onMoveAlongX(event, dx);
	}

	@Override
	public void onMoveAlongY(ViewportChangeEvent event, double dy) {
		if(!this.CartesianChartModuleGWT.canHandleEvents)
			return;
		if(!isAddressed(event))
			return;
		if(this.CartesianChartModuleGWT.getYAxis().getModulToAlign() == this.CartesianChartModuleGWT){
			this.CartesianChartModuleGWT.getYAxis().setMin(this.CartesianChartModuleGWT.getYAxis().getMin() + dy);
			this.CartesianChartModuleGWT.getYAxis().setMax(this.CartesianChartModuleGWT.getYAxis().getMax() + dy);

		}
		this.CartesianChartModuleGWT.onMoveAlongY(event, dy);
	}

	@Override
	public void onSetX(ViewportChangeEvent event, double xMin, double xMax) {
		if(!this.CartesianChartModuleGWT.canHandleEvents)
			return;
		if(!isAddressed(event))
			return;
		if(this.CartesianChartModuleGWT.getXAxis().getModulToAlign() == this.CartesianChartModuleGWT){
			this.CartesianChartModuleGWT.getXAxis().setMin(xMin);
			this.CartesianChartModuleGWT.getXAxis().setMax(xMax);

		}
		this.CartesianChartModuleGWT.onSetX(event, xMin, xMax);
	}

	@Override
	public void onSetY(ViewportChangeEvent event, double yMin, double yMax) {
		if(!this.CartesianChartModuleGWT.canHandleEvents)
			return;
		if(!isAddressed(event))
			return;
		if(this.CartesianChartModuleGWT.getYAxis().getModulToAlign() == this.CartesianChartModuleGWT){
			this.CartesianChartModuleGWT.getYAxis().setMin(yMin);
			this.CartesianChartModuleGWT.getYAxis().setMax(yMax);

		}
		this.CartesianChartModuleGWT.onSetY(event, yMin, yMax);
	}

	@Override
	public void onDataSetChange(DataSetChangeEvent event) {
		this.CartesianChartModuleGWT.onDataSetChange(event);
	}


	@Override
	public void onSelect(DataEntrySelectionEvent event) {
		this.CartesianChartModuleGWT.onSelect(event);			
	}


	@Override
	public void onDeselect(DataEntrySelectionEvent event) {
		this.CartesianChartModuleGWT.onDeselect(event);			
	}

	@Override
	public void beforeShow(RectangleTile tile) {
		CartesianChartModuleGWT.beforeShow((CartesianRectangleTile) tile);		
	}

	@Override
	public void beforeHide(RectangleTile tile) {
		CartesianChartModuleGWT.beforeShow((CartesianRectangleTile) tile);
	}

}